home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue24 / tiptrix / LISTING4.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-07-21  |  958 b   |  38 lines

  1. TYPE
  2.   charset  = Set of Char;
  3. FUNCTION get_nth_word(wrkstr : STRING; nth_word : BYTE;
  4.   Delim : charset) : STRING;
  5. VAR
  6.   i, ndx, start_pt, end_pt : BYTE;
  7.   found : BOOLEAN;
  8. BEGIN
  9.   start_pt := 1;
  10.   end_pt := length(wrkstr);
  11.   IF LENGTH(wrkstr) = 0 THEN BEGIN
  12.     result := æÆ;         { whoops, empty string}
  13.     EXIT;
  14.   END;
  15.   ndx := 1;
  16.   FOR i := 1 TO nth_word DO BEGIN
  17.     found := FALSE;
  18.     REPEAT                      { find first delimiter }
  19.       IF wrkstr[ndx] in delim THEN
  20.         INC(ndx)
  21.       ELSE BEGIN
  22.         start_pt := ndx;
  23.         found := TRUE;
  24.       END;
  25.     UNTIL found;
  26.     found := FALSE;
  27.     REPEAT                      { find end of this word }
  28.       IF (ndx > LENGTH(wrkstr)) OR (wrkstr[ndx] in delim)
  29.         THEN BEGIN
  30.         end_pt := ndx - 1;
  31.         found := TRUE;
  32.       END ELSE
  33.         INC(ndx);
  34.     UNTIL found;
  35.   END;
  36.   result := COPY(wrkstr, start_pt, end_pt - start_pt + 1);
  37. END;
  38.